<template>
  <div class="root">
    <div class="container" id="container">
      <div class="form-container sign-up-container">
        <div class="form">
          <h3 style="color: beige; margin-top: 20%">使用您的手机号码注册</h3>
          <input
            type="text"
            placeholder="姓名"
            style="margin-top: 5%"
            onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
            autocomplete="“off“"
            v-model="regData.name"
            @input="regNameInput"
          />
          <h6
            :style="
              !regCheck.name ? regCheck.styleObject : regCheck.okStyleObject
            "
          >
            {{ !regCheck.name ? "请输入不大于4个字符名字" : "校验通过" }}
          </h6>
          <input
            type="text"
            placeholder="店铺名字"
            v-if="isStore"
            onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
            autocomplete="“off“"
            v-model="regData.storeName"
            @input="regStoreNameInput"
          />
          <h6
            :style="
              !regCheck.storeName
                ? regCheck.styleObject
                : regCheck.okStyleObject
            "
            v-if="isStore"
          >
            {{
              !regCheck.storeName
                ? "请输入不大于10个字符的店铺名字"
                : "校验通过"
            }}
          </h6>
          <input
            type="text"
            placeholder="手机"
            onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
            autocomplete="“off“"
            v-model="regData.phone"
            @input="regPhoneInput"
          />
          <h6
            :style="
              !regCheck.phone ? regCheck.styleObject : regCheck.okStyleObject
            "
          >
            {{ !regCheck.phone ? "请输入正确的手机号" : "校验通过" }}
          </h6>
          <input
            type="password"
            placeholder="密码"
            onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
            autocomplete="“off“"
            v-model="regData.password"
            @input="regPasswordInput"
          />
          <h6
            :style="
              !regCheck.password ? regCheck.styleObject : regCheck.okStyleObject
            "
          >
            {{ !regCheck.password ? "请输入6-16位密码" : "校验通过" }}
          </h6>
          <input
            type="text"
            placeholder="验证码"
            v-model="regData.keyWord"
            @input="regKeyWordInput"
          />
          <h6
            :style="
              !regCheck.keyWord ? regCheck.styleObject : regCheck.okStyleObject
            "
          >
            {{ !regCheck.keyWord ? "请输入验证码" : "通过" }}
          </h6>
          <input
            type="text"
            placeholder="地址（请从地图上选择）"
            disabled="true"
            v-model="mapInfo.userInputAddress"
          />
          <el-radio-group
            v-model="regData.identity"
            fill="#ff4b2b"
            style="margin-top: 2%"
          >
            <el-radio-button :label="0">注册零售量</el-radio-button>
            <el-radio-button :label="1">注册销售商</el-radio-button>
          </el-radio-group>
          <button style="margin-top: 2%" @click="regUser">注册</button>
          <div :class="{ 'gender-store': isStore, 'gender-nostore': !isStore }">
            <el-radio-group v-model="regData.gender" fill="#ff4b2b" size="mini">
              <el-radio-button :label="0">先生</el-radio-button>
              <el-radio-button :label="1">女士</el-radio-button>
            </el-radio-group>
          </div>
          <div :class="{ 'key-store': isStore, 'key-nostore': !isStore }">
            <el-button
              type="primary"
              size="small"
              style="width: 70%; background-color: #ff4b2b; border: #ff4b2b"
              @click="getKey"
              :disabled="isDisabled"
              ><h6 style="margin-left: -110%">
                {{ !isDisabled ? "获取验证码" : seconds + "秒有效" }}
              </h6></el-button
            >
          </div>
          <div class="map">
            <el-button
              type="primary"
              size="small"
              style="width: 70%; background-color: #ff4b2b"
              @click="getMap"
              ><h6 style="margin-left: -200%">选择地址</h6></el-button
            >
          </div>
        </div>
      </div>
      <div class="form-container sign-in-container">
        <div class="form">
          <h1 style="margin-top: -10%">BIT碧涛超级供销系统</h1>
          <h3 style="color: beige; margin-top: 2%">使用手机登录</h3>
          <input
            type="text"
            placeholder="电话"
            v-model="loginData.phone"
            style="margin-top: 15%"
            @input="loginPhoneInput"
            onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
            autocomplete="“off”"
          />
          <h6
            :style="
              !loginCheck.phone
                ? loginCheck.styleObject
                : loginCheck.okStyleObject
            "
          >
            {{ !loginCheck.phone ? "请输入正确的手机号" : "校验通过" }}
          </h6>
          <input
            type="password"
            placeholder="密码"
            v-model="loginData.password"
            onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
            autocomplete="“off”"
            @input="loginPasswordInput"
          />
          <h6
            :style="
              !loginCheck.password
                ? loginCheck.styleObject
                : loginCheck.okStyleObject
            "
          >
            {{
              !loginCheck.password
                ? "请输入6-16位密码,不能含有空格"
                : "校验通过"
            }}
          </h6>
          <button style="margin-top: 25%; cursor: pointer" @click="login">
            登录
          </button>
        </div>
      </div>
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <h1>欢迎回来</h1>
            <p>与我们保持联系请登录您的个人信息</p>
            <button class="ghost" id="signIn">登录</button>
          </div>
          <div class="overlay-panel overlay-right">
            <h1>欢迎光临</h1>
            <p>输入您的个人资料，并与我们一起开始旅程</p>
            <button class="ghost" id="signUp">注册</button>
          </div>
        </div>
      </div>
    </div>
    <div id="aa"></div>
    <el-dialog
      title="小提示：可以通过拖动小红点来选取地址"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
      :modal-append-to-body="false"
      append-to-body
    >
      <BMapSearch bLeft="-48%" bWidth="100%" @submitMap="getMapAddress" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setMapAddress">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { userLogin, userRegister } from "@/api/userApi";
import { COOKIE_TIME } from "@/utils/userCookie";
import BMapSearch from "@/components/BMapSearch";
import { getCode } from "@/api/aliyunApi";
export default {
  components: {
    BMapSearch,
  },
  data() {
    return {
      radio: 1,
      centerDialogVisible: false,
      seconds: 119,
      isDisabled: false,
      loginData: {
        phone: "",
        password: "",
        identity: 0,
      },
      regData: {
        name: "",
        storeName: "",
        phone: "",
        password: "",
        keyWord: "",
        gender: 0,
        identity: 0,
      },
      loginCheck: {
        phone: false,
        password: false,
        styleObject: {
          color: "red",
        },
        okStyleObject: {
          color: "green",
        },
      },
      regCheck: {
        name: false,
        phone: false,
        password: false,
        keyWord: false,
        storeName: false,
        styleObject: {
          color: "red",
        },
        okStyleObject: {
          color: "green",
        },
      },
      mapInfoTemp: null,
      mapInfo: {
        userInputAddress: null,
      },
    };
  },
  computed: {
    isStore() {
      return this.regData.identity == 0 ? false : true;
    },
  },
  methods: {
    /*
    登陆验证
    */
    loginPhoneInput() {
      let regu = /^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])|(18[0-9])|(19[8,9]))\d{8}$/;
      // let re = new RegExp(regu);

      if (this.loginData.phone.match(regu) != null) {
        this.loginCheck.phone = true;
      } else {
        this.loginCheck.phone = false;
      }
    },
    loginPasswordInput() {
      if (
        this.loginData.password.length > 5 &&
        this.loginData.password.length < 17
      ) {
        this.loginCheck.password = true;
      } else {
        this.loginCheck.password = false;
      }
    },

    /*
    注册验证
    */
    regStoreNameInput() {
      if (
        this.regData.storeName.length > 0 &&
        this.regData.storeName.length < 11
      ) {
        this.regCheck.storeName = true;
      } else {
        this.regCheck.storeName = false;
      }
    },
    regNameInput() {
      if (this.regData.name.length > 0 && this.regData.name.length < 5) {
        this.regCheck.name = true;
      } else {
        this.regCheck.name = false;
      }
    },
    regPhoneInput() {
      /**
       * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
       * 此方法中前三位格式有：
       * 13+任意数
       * 145,147,149
       * 15+除4的任意数(不要写^4，这样的话字母也会被认为是正确的)
       * 166
       * 17+3,5,6,7,8
       * 18+任意数
       * 198,199
       */
      let regu = /^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])|(18[0-9])|(19[8,9]))\d{8}$/;
      // let re = new RegExp(regu);

      if (this.regData.phone.match(regu) != null) {
        this.regCheck.phone = true;
      } else {
        this.regCheck.phone = false;
      }
    },
    regPasswordInput() {
      if (
        this.regData.password.length > 5 &&
        this.regData.password.length < 17
      ) {
        this.regCheck.password = true;
      } else {
        this.regCheck.password = false;
      }
    },
    regKeyWordInput() {
      this.regCheck.keyWord = this.regData.keyWord.length > 0;
    },
    /**
     *
     * 业务逻辑：
     */
    login() {
      if (this.loginCheck.phone && this.loginCheck.password) {
        const loading = this.$loading({
          lock: true,
          text: "登陆中",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        userLogin(this.loginData).then((res) => {
          if (res.code == 0) {
            this.$cookie.set("token", res.data.token, COOKIE_TIME);
            this.$notify.success({
              title: "成功",
              message: "登陆成功",
              duration: 2000,
            });
            this.$router.push("/");
            loading.close();
          } else {
            this.$message.error("登录失败：" + res.message);
            loading.close();
          }
        }).catch(err => {
           this.$message.error("登录失败：" + err);
           loading.close();
        });
      } else {
        this.$message.error("数据校验不通过，请仔细检查");
      }
    },
    regUser() {
      /**
       * name: false,
        phone: false,
        password: false,
        keyWord: false,
        map: false,
        storeName: false,
       */
      let submit =
        this.regCheck.name &&
        this.regCheck.phone &&
        this.regCheck.password &&
        this.regCheck.keyWord &&
        this.mapInfo.userInputAddress != null &&
        (this.regData.identity == 0 ||
          (this.regData.identity == 1 && this.regCheck.storeName));

      if (submit) {
        let data = { ...this.regData, ...this.mapInfo };
        const loading = this.$loading({
          lock: true,
          text: "正在注册中请稍后",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        userRegister(data)
          .then((res) => {
            loading.close();
            if (res.code == 0) {
              this.$alert("恭喜您注册账号成功", "成功", {
                confirmButtonText: "立即登录",
                callback: () => {
                  window.location.reload();
                },
              });
            } else if (res.code == 451) {
              this.$alert("验证码错误", "失败", {
                confirmButtonText: "确定",
                callback: () => {
                   this.$message.info("请输入正确的验证码");
                },
              });
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
            loading.close();
          });
      } else {
        this.$message.error("数据校验不通过，请仔细检查");
      }
    },
    getMap() {
      this.centerDialogVisible = true;
    },
    getKey() {
      let _self = this;
      if (this.regCheck.phone) {
        getCode(this.regData.phone,1)
          .then((res) => {
            if (res.code == 0) {
              _self.isDisabled = true;
              let keyTime = setInterval(function () {
                if (_self.seconds == 0) {
                  _self.seconds = 119;
                  _self.isDisabled = false;
                  window.clearInterval(keyTime);
                } else {
                  _self.seconds--;
                }
              }, 1000);
              this.$message.success("验证码已发送，两分钟内有效");
            } else {
              this.$message.error(res.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      } else {
        this.$message.error("请输入手机号");
      }
    },
    init() {
      const signUpButton = document.getElementById("signUp");
      const signInButton = document.getElementById("signIn");
      const container = document.getElementById("container");

      signUpButton.addEventListener("click", () => {
        container.classList.add("right-panel-active");
      });

      signInButton.addEventListener("click", () => {
        container.classList.remove("right-panel-active");
      });
    },
    getMapAddress(data) {
      this.mapInfoTemp = data;
    },
    setMapAddress() {
      this.mapInfo = this.mapInfoTemp;

      this.centerDialogVisible = false;
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="scss" scoped>
#l-map {
  width: 100%;
  height: 250px;
  border: 1px solid red;
}
* {
  box-sizing: border-box;
}
.gender-store {
  position: relative;
  top: -85%;
  left: 48%;
}
.gender-nostore {
  position: relative;
  top: -73%;
  left: 48%;
}
.key-store {
  position: relative;
  top: -42%;
  left: 48%;

  /* border: 1px solid green; */
}
.key-nostore {
  position: relative;
  top: -42%;
  left: 48%;
}
.map {
  position: relative;
  top: -36%;
  left: 48%;
}
.root {
  font-family: "Montserrat", sans-serif;
  background: #f6f5f7;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  //   margin: -20px 0 50px;
  background-image: url(/images/view-of-earth-map.jpg);
  background-size: cover;
}

h1 {
  font-weight: bold;
  margin: 0;
  color: beige;
}

p {
  font-size: 14px;
  font-weight: bold;
  line-height: 20px;
  letter-spacing: 0.5px;
  margin: 20px 0 30px;
}

span {
  font-size: 12px;
  color: beige;
}

a {
  color: #fff;
  font-size: 14px;
  text-decoration: none;
  margin: 15px 0;
}

.container {
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  position: absolute;
  overflow: hidden;
  width: 1050px;
  max-width: 100%;
  min-height: 600px;
  opacity: 0.8;
}

.form-container form {
  background: rgba(45, 52, 54, 1);
  display: flex;
  flex-direction: column;
  padding: 0 50px;
  height: 100%;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.form-container .form {
  background: rgba(45, 52, 54, 1);
  display: flex;
  flex-direction: column;
  padding: 0 50px;
  height: 100%;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.social-container {
  margin: 20px 0;
}

.social-container a {
  border: 1px solid #ddd;
  border-radius: 50%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin: 0 5px;
  height: 40px;
  width: 40px;
}

.form-container input {
  background: #eee;
  border: none;
  padding: 12px 15px;
  margin: 8px 0;
  width: 100%;
}

button {
  border-radius: 20px;
  border: 1px solid #ff4b2b;
  background: #ff4b2b;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  padding: 12px 45px;
  letter-spacing: 1px;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
  cursor: pointer;
}

input[type="text"] {
  width: 300px;
  text-align: center;
  background: transparent;
  border: none;
  border-bottom: 1px solid #fff;
  font-family: "PLay", sans-serif;
  font-size: 16px;
  font-weight: 200px;
  padding: 10px 0;
  transition: border 0.5s;
  outline: none;
  color: #fff;
  font-weight: bold;
}

input[type="password"] {
  width: 300px;
  text-align: center;
  background: transparent;
  border: none;
  border-bottom: 1px solid #fff;
  font-family: "PLay", sans-serif;
  font-size: 16px;
  font-weight: bold;
  padding: 10px 0;
  transition: border 0.5s;
  outline: none;
  color: #fff;
}

input[type="email"] {
  width: 240px;
  text-align: center;
  background: transparent;
  border: none;
  border-bottom: 1px solid #fff;
  font-family: "PLay", sans-serif;
  font-size: 16px;
  font-weight: 200px;
  padding: 10px 0;
  transition: border 0.5s;
  outline: none;
  color: #fff;
  font-weight: bold;
}

button:active {
  transform: scale(0.95);
}

button:focus {
  outline: none;
}

button.ghost {
  background: transparent;
  border-color: #ff4b2b;
  background-color: #ff4b2b;
}

.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
}

.sign-in-container {
  left: 0;
  width: 50%;
  z-index: 2;
}

.sign-up-container {
  left: 0;
  width: 50%;
  z-index: 1;
  opacity: 0;
}

.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}

.overlay {
  background: transparent;
  background: linear-gradient(to right, #ff4b2b, #ff416c) no repeat 0 0 / cover;
  color: #fff;
  position: absolute;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-panel {
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 40px;
  height: 100%;
  width: 50%;
  text-align: center;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-right {
  right: 0;
  transform: translateX(0);
}

.overlay-left {
  transform: translateX(-20%);
}

/*....Animation....*/

/*....Move signin to the right....*/
.container.right-panel-active .sign-in-container {
  transform: translateX(100%);
}

/*....Move overlay to the left....*/
.container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}

/*....Bring sign up over sign in....*/
.container.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 5;
}

/*...Move overlay back to right....*/
.container.right-panel-active .overlay {
  transform: translateX(50%);
}

.container.right-panel-active .overlay-left {
  transform: translateX(0);
}

.container.right-panel-active .overlay-right {
  transform: translateX(20%);
}
</style>